import { createRouter, createWebHashHistory } from 'vue-router';
import { MenuDataItem } from './typing';
import { loginRoutePath } from './define-meta';
// import { GENERATE_ROUTES, GET_USER } from '@/store/modules/user/actions';
import {GET_USER } from '@/store/modules/user/actions';
import {Component} from"vue"
import store from '@/store'
import { getToken } from '@/utils/user-util'
import ls from '@/utils/local-storage';
import { STORAGE_TOKEN_KEY } from '@/store/mutation-type';
import { state as initState, state } from '@/store/modules/user/state';
export const routes: MenuDataItem[] = [
  {
    name: 'test',
    path: '/test',
    component: (): Component => import('@/views/test/test.vue'),
    meta: {
      title: '测试',
      name:'test'
    },
  },
  {
    name: 'success',
    path: '/success',
    component: (): Component => import('@/views/workflow/success/index.vue'),
    meta: {
      title: '提交成功',
      name:'success'
    },
  },
  {
    name: 'yuanfucaishouhuo',
    path: '/yuanfucaishouhuo',
    component: (): Component => import('@/views/workflow/yuanfucaishouhuo/index.vue'),
    meta: {
      title: '原辅材收货',
      name:'yuanfucaishouhuo'
    },
  },
  {
    name: 'yhxiaoshoufahuo',
    path: '/yhxiaoshoufahuo',
    component: (): Component => import('@/views/workflow/yhxiaoshoufahuo/index.vue'),
    meta: {
      title: '原辅材收货',
      name:'yhxiaoshoufahuo'
    },
  },
  {
    name: 'wangongshenpi',
    path: '/wangongshenpi',
    component: (): Component => import('@/views/workflow/wangongshenpi/index.vue'),
    meta: {
      title: '完工审批',
      name:'wangongshenpi'
    },
  },
];
const router = createRouter({
  //history: createWebHistory(process.env.VUE_APP_PUBLIC_PATH),
  history: createWebHashHistory(),
  routes
});
router.beforeEach(async to => {
  const userToken = getToken();
  if (!userToken) {
    const getQueryVariable=(variable:any)=>{
      var arr = window.location.href.split('?')
      console.log(window.location,'window.location');
      if (arr.length <= 1) return '';
      var query = arr[1];
      var vars = query.split("&");
      for (var i=0;i<vars.length;i++) {
              var pair = vars[i].split("=");
              if(pair[0] == variable){return pair[1];}
      }
      return ''
   }
    let token= getQueryVariable('token')
    if (token && token.length > 0){
            state.token = token;
            window.localStorage.access_token =token;
            ls.set(STORAGE_TOKEN_KEY, token);
    }
    // 验证是否有通行证
    if (to.meta?.through) {
      return true;
    }
    // if (to.fullPath !== loginRoutePath&&(to.fullPath.indexOf("token") == -1)) {
    //   // 未登录，进入到登录页
    //   return {
    //     path: loginRoutePath,
    //     replace: true,
    //   };
    // }
    return to;
  }
  if(userToken){
    return true
  }

  // if (store.getters['user/routers'] && store.getters['user/routers'].length > 0) {
  //   return true;
  // } else {
  //   await store.dispatch(`user/${GET_USER}`);
  //   // const routers = await store.dispatch(`user/${GENERATE_ROUTES}`);
  //   // if (routers) {
  //   //   return { ...to, replace: true };
  //   // }
  //   return false;
  // }
});
router.afterEach(() => { });
export default router;
